
$(function() {
	
	$('#date').datepicker(_date1_option);
	
	$('#date1').datepicker(_date1_option);
	$('#date2').datepicker(_date1_option);
	
	$('#date1').change(function() {
		_cal();
	});
	
	$('#date2').change(function() {
		_cal();
	});
	
	$('#day_t').change(function() {
		var s = $(this).val();
		if (s != null && s != '-')　{
			$('#day').val(s);
		}
		
		_day();
	});
	
	$('#day').change(function() {
		_day();
	});
	
	var options = {
			editable: true,
		    enableAddRow: true,
		    enableCellNavigation: true,
		    asyncEditorLoading: false,
		    rowHeight: 30
		};
	
	var grid = new Slick.Grid($("#myGrid"), data, columns, options);
	
	grid.onAddNewRow.subscribe(function (e, args) {
		
        var item = {currency: "CNY", amount: "1", ok: true};
        
        //item.date = current_date();
    	
        $.extend(item, args.item);
        data.push(item);
        grid.invalidateRows([data.length - 1]);
        grid.updateRowCount();
        grid.render();
    });
	
	grid.onCellChange.subscribe(function(e, args) {
		item = args.item;
		currentCell = args.cell;
		currentRow = args.row;
    	
    	grid.updateRow(currentRow);
    	grid.updateRowCount();
        grid.render();
    });
});


function _day() {
	var t1 = $('#date1').val();
	var dd = $('#day').val();
		
	t1 = t1.replace(/\-/g, "/");
	
	var date = new Date(t1);
	date.setDate(date.getDate() + parseInt(dd));
	
	var dt = $.dateFormat.date(date, "yyyy-MM-dd");
	var t1 = $('#date2').val(dt);
    // alert(dt);
	
	_cal();
}

function _cal() {
	var t1 = $('#date1').val();
	// alert(t1);
	if (t1 == undefined || t1 == '') {
		return;
	}
	
	var t2 = $('#date2').val();
	// alert(t2);
	if (t2 == undefined || t2 == '') {
		return;
	}
	
	var day = GetDateDiff(t1, t2, "day");
	if (day <= 0) {
		return ;
	}
	
	$('#day').val(day);
}